﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Site.Mobile
{
    public partial class EditHive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                SqlDataSource1.UpdateCommand = "UPDATE Hives SET Hive_ID=@HID, WID=@WID, Location=@Location, Notes=@Notes WHERE Hive_ID='" + edtHID.Text + "'";
                SqlDataSource1.UpdateParameters.Add("HID", edtHID.Text);
                SqlDataSource1.UpdateParameters.Add("WID", edtWID.Text);
                SqlDataSource1.UpdateParameters.Add("Location", edtLong.Text + " " + edtLat.Text);
                SqlDataSource1.UpdateParameters.Add("Notes", edtNotes.Text);
                SqlDataSource1.Update();
                Response.Redirect("Hives.aspx");
            }

            string sHID = Request.QueryString["HID"];
            if (sHID != null)
            {
                SqlDataSource1.SelectCommand = "SELECT * FROM Hives WHERE Hive_ID='" + sHID + "'";
                DataView View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());

                if (View.Count == 1)
                {
                    edtHID.Text = View[0]["Hive_ID"].ToString();
                    edtWID.Text = View[0]["WID"].ToString();

                    string sPos = View[0]["Location"].ToString();
                    if (sPos != "")
                    {
                        edtLong.Text = sPos.Substring(0, sPos.IndexOf(' '));
                        edtLat.Text = sPos.Replace(edtLong.Text + " ", "");
                    }
                    edtNotes.Text = View[0]["Notes"].ToString();

                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }

        int FindIndexOf(string Value)
        {
            for (int x = 0; x < edtWID.Items.Count; x++)
            {
                if (edtWID.Items[x].Text == Value)
                    return x;
            }

            return -1;
        }
    }
}